<?php 

class Log {

    private $nomeArquivoLog = '';

    // define default log file
    private $pastaLogs = 'logs';

    private $extensaoLog = '.log';

    // define file pointer
    private $fp = null;

    public function escrever($mensagem, $codigo, $arquivo, $linha) {
        // if file pointer doesn't exist, then open log file
        if (!$this->fp) {
            $this->abrir();
        }
        // define current time
        $time = date('H:i:s');
        // write current time, script name and message to the log file
        // in case of using on Windows, instead of "\n" use "\r\n"
        $msg = $time.' | '.$mensagem.' | '.$arquivo.' | linha '.$linha. " \n";
        
        fwrite($this->fp, $msg);

        unset($msg, $time);
    }


    public function fechar() {
        fclose($this->fp);
    }

    private function abrir() {
        $arquivo = $this->pastaLogs . DS . date('Y-m-d'). $this->extensaoLog;
        // open log file for writing only; place the file pointer at the end of the file
        // if the file does not exist, attempt to create it
        $this->fp = fopen($arquivo, 'ab') or die('Impossívle criar ou abrir '.$arquivo);
    }
}

?>